package com.example.flow.flow.service;

import com.example.flow.dao.FlowOrderDao;
import com.example.flow.dao.FlowOrderHisDao;
import com.example.flow.dao.model.FlowOrder;
import com.example.flow.dao.model.FlowOrderHis;
import com.example.flow.dao.model.FlowProcess;
import com.example.flow.utils.GsonUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

import static com.example.flow.flow.FlowConstant.STATE_ACTIVE;
import static com.example.flow.flow.FlowConstant.STATE_FINISH;

/**
 * 流程工单
 *
 * @author tangaq
 * 2023/6/13
 */
@Service
public class FlowOrderService {
    private final FlowOrderDao orderDao;
    private final FlowOrderHisDao orderHisDao;

    public FlowOrderService(FlowOrderDao orderDao,
                            FlowOrderHisDao hisOrderDao) {
        this.orderDao = orderDao;
        this.orderHisDao = hisOrderDao;
    }

    /**
     * 创建流程工单
     */
    public FlowOrder createOrder(FlowProcess process, Map<String, Object> args) {
        FlowOrder order = new FlowOrder();
        order.setOrderId(UUID.randomUUID().toString().replace("-",""));
        order.setProcessId(process.getProcessId());
        if (args != null) {
            order.setArgs(GsonUtil.obj2Json(args));
        }
        orderDao.insert(order);

        FlowOrderHis history = new FlowOrderHis(order);
        history.setHisOrderId(UUID.randomUUID().toString().replace("-",""));
        history.setOrderState(STATE_ACTIVE);
        orderHisDao.insert(history);
        return order;
    }

    public FlowOrder findByOrderId(String orderId) {
        return orderDao.findByOrderId(orderId);
    }

    /**
     * 删除活动流程实例 更新历史流程实例状态、结束时间
     *
     * @param orderId
     */
    public void complete(String orderId) {
        FlowOrder order = this.findByOrderId(orderId);
        FlowOrderHis history = orderHisDao.findByOrderId(orderId);
        history.setOrderState(STATE_FINISH);
//        history.setFinishTime(DateUtils.getCurrentTime(DateUtils.DATE_TIME_FORMAT));
//        orderHisDao.upgradeById(history.getId(), history);

//        orderDao.deleteById(order.getId());
    }

    public void deleteByOrderId(String orderId) {
        orderDao.deleteByOrderId(orderId);
        orderHisDao.deleteByOrderId(orderId);
    }

    /**
     * 重新发起已完成的历史任务
     *
     * @param taskId
     * @return
     */
//    public FlowOrder resume(String orderId) {
//        FlowOrderHis historyOrder = hisOrderDao.findByOrderId(orderId);
//        FlowOrder order = historyOrder.undo();
//        orderDao.insert(order);
//        historyOrder.setOrderState(STATE_ACTIVE);
//        hisOrderDao.upgradeById(historyOrder.getId(), historyOrder);
//
//        List<FlowTaskHis> histTasks = taskHisDao.getHistoryTasks(null, orderId);
//        if (histTasks != null && !histTasks.isEmpty()) {
//            FlowTaskHis histTask = histTasks.get(0);
//            taskService.resume(histTask.getId());
//        }
//        return order;
//    }
}
